<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        *{
            margin: 0;
            padding: 0;
        }
        div{
            width: 200px;
            height: 200px;
            background: red;
            
        }
        aside{
            position: fixed;
            top: 0;
            left: 0;
            right: 0;
            bottom: 0;
            margin: auto;
            width: 200px;
            height: 100px;
            display: flex;
        }
        aside button{
            font-size: 26px;
            width: 100px;
        }
    </style>
</head>
<body>
    <div></div>
    <aside>
        <button>开始</button>
        <button>停止</button>
    </aside>
    <script>
        //获取元素
        var box = document.querySelector("div")
        var btn1 = document.querySelector("aside button:nth-of-type(1)")
        var btn2 = document.querySelector("aside button:nth-of-type(2)")
        //tmp是位移  tmp1是停止 tmp2是计数
        var tmp = 0
        var tmp1 = 0
        var tmp2 = 0

        btn1.onclick = function () {
            tmp1--
            tmp2++
            console.log(tmp2);
            if(tmp2 >= 2){
                return
            }
            fn()
        }
        btn2.onclick = function(){
            tmp1 = tmp   
            tmp2 = 0
         }

         //判断
        function fn(){ 
            if(tmp1 < tmp){
                fn1(tmp += 10)
                setTimeout(fn,1000)
             }
        }
        //位移
        function fn1(){
            box.style.transform = `translate(${tmp}px)`
        }
    </script>
</body>
</html>